/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "../ed_local.h"
#include "../../win32/win_local.h"

#define LIGHT_EDITOR_WINDOW_NAME		ENGINE_NAME " Light Editor"
#define LIGHT_EDITOR_WINDOW_CLASS		ENGINE_NAME " LightEditorWnd"
#define LIGHT_EDITOR_WINDOW_STYLE		(WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)

#define LIGHT_COLOR_BITMAP_SIZE			66 * 20

static lightProperties_t	ed_lightProperties = {odVec3(0.0f, 0.0f, 0.0f), odVec3(0.0f, 0.0f, 0.0f), odAngles(0.0f, 0.0f, 0.0f), odVec3(100.0f, 100.0f, 100.0f), 90.0f, 90.0f, 200.0f, false, false, false, 0, "_defaultPointLight", 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f};

typedef struct {
	bool					enabled;

	lightCallbacks_t		callbacks;

	int						editIndex;
	lightProperties_t		editProperties;

	int						currentTransform;

	dword					bitmapBits[LIGHT_COLOR_BITMAP_SIZE];

	// Window stuff
	HWND					hWnd;

	HWND					hWndFrame1;
	HWND					hWndFrame2;
	HWND					hWndFrame3;
	HWND					hWndFrame4;
	HWND					hWndFrame5;

	HWND					hWndX;
	HWND					hWndY;
	HWND					hWndZ;

	HWND					hWndOrigin;
	HWND					hWndOriginX;
	HWND					hWndOriginY;
	HWND					hWndOriginZ;

	HWND					hWndCenter;
	HWND					hWndCenterX;
	HWND					hWndCenterY;
	HWND					hWndCenterZ;

	HWND					hWndAngles;
	HWND					hWndAnglesX;
	HWND					hWndAnglesY;
	HWND					hWndAnglesZ;

	HWND					hWndTransform;
	HWND					hWndTransformValue;

	HWND					hWndXInc;
	HWND					hWndXDec;
	HWND					hWndYInc;
	HWND					hWndYDec;
	HWND					hWndZInc;
	HWND					hWndZDec;

	HWND					hWndPointLight;

	HWND					hWndRadius;
	HWND					hWndRadiusX;
	HWND					hWndRadiusY;
	HWND					hWndRadiusZ;

	HWND					hWndProjectedLight;

	HWND					hWndFov;
	HWND					hWndFovXValue;
	HWND					hWndFovYValue;

	HWND					hWndFalloffRange;
	HWND					hWndFalloffRangeValue;

	HWND					hWndParallel;

	HWND					hWndCastShadows;

	HWND					hWndDetailLevel;
	HWND					hWndDetailLevelValue;

	HWND					hWndColor;
	HWND					hWndColorSelect;

	HWND					hWndAlpha;
	HWND					hWndAlphaValue;

	HWND					hWndDepth;
	HWND					hWndDepthValue;

	HWND					hWndMaterial;
	HWND					hWndMaterialName;

	HWND					hWndApply;
	HWND					hWndReset;
	HWND					hWndRemove;
	HWND					hWndSave;

	HFONT					hFont;

	HBITMAP					hBitmap;

	COLORREF				crCurrent;
	COLORREF				crCustom[16];
} lightEditor_t;

static lightEditor_t		ed_lightEditor;


/*
 ==================
 ED_AddLightMaterial
 ==================
*/
static void ED_AddLightMaterial (const char *name){

	if (!Str_ICompareChars("lights/", name, 7)){
		if (SendMessage(ed_lightEditor.hWndMaterialName, CB_FINDSTRINGEXACT, -1, (LPARAM)name) != CB_ERR)
			return;

		SendMessage(ed_lightEditor.hWndMaterialName, CB_ADDSTRING, 0, (LPARAM)name);
		return;
	}

	if (!Str_ICompareChars("fogs/", name, 5)){
		if (SendMessage(ed_lightEditor.hWndMaterialName, CB_FINDSTRINGEXACT, -1, (LPARAM)name) != CB_ERR)
			return;

		SendMessage(ed_lightEditor.hWndMaterialName, CB_ADDSTRING, 0, (LPARAM)name);
		return;
	}
}

/*
 ==================
 ED_ApplyLightProperties
 ==================
*/
static void ED_ApplyLightProperties (void){

	lightProperties_t	properties;
	char				string[32];

	if (!ed_lightEditor.enabled)
		return;

	// Read the controls
	if (GetWindowText(ed_lightEditor.hWndOriginX, string, sizeof(string)))
		properties.origin[0] = Str_ToFloat(string);
	else
		properties.origin[0] = ed_lightEditor.editProperties.origin[0];

	if (GetWindowText(ed_lightEditor.hWndOriginY, string, sizeof(string)))
		properties.origin[1] = Str_ToFloat(string);
	else
		properties.origin[1] = ed_lightEditor.editProperties.origin[1];

	if (GetWindowText(ed_lightEditor.hWndOriginZ, string, sizeof(string)))
		properties.origin[2] = Str_ToFloat(string);
	else
		properties.origin[2] = ed_lightEditor.editProperties.origin[2];

	if (GetWindowText(ed_lightEditor.hWndCenterX, string, sizeof(string)))
		properties.center[0] = Str_ToFloat(string);
	else
		properties.center[0] = ed_lightEditor.editProperties.center[0];

	if (GetWindowText(ed_lightEditor.hWndCenterY, string, sizeof(string)))
		properties.center[1] = Str_ToFloat(string);
	else
		properties.center[1] = ed_lightEditor.editProperties.center[1];

	if (GetWindowText(ed_lightEditor.hWndCenterZ, string, sizeof(string)))
		properties.center[2] = Str_ToFloat(string);
	else
		properties.center[2] = ed_lightEditor.editProperties.center[2];

	if (GetWindowText(ed_lightEditor.hWndAnglesX, string, sizeof(string)))
		properties.angles[0] = Str_ToFloat(string);
	else
		properties.angles[0] = ed_lightEditor.editProperties.angles[0];

	if (GetWindowText(ed_lightEditor.hWndAnglesY, string, sizeof(string)))
		properties.angles[1] = Str_ToFloat(string);
	else
		properties.angles[1] = ed_lightEditor.editProperties.angles[1];

	if (GetWindowText(ed_lightEditor.hWndAnglesZ, string, sizeof(string)))
		properties.angles[2] = Str_ToFloat(string);
	else
		properties.angles[2] = ed_lightEditor.editProperties.angles[2];

	if (GetWindowText(ed_lightEditor.hWndRadiusX, string, sizeof(string)))
		properties.radius[0] = Str_ToFloat(string);
	else
		properties.radius[0] = ed_lightEditor.editProperties.radius[0];

	if (GetWindowText(ed_lightEditor.hWndRadiusY, string, sizeof(string)))
		properties.radius[1] = Str_ToFloat(string);
	else
		properties.radius[1] = ed_lightEditor.editProperties.radius[1];

	if (GetWindowText(ed_lightEditor.hWndRadiusZ, string, sizeof(string)))
		properties.radius[2] = Str_ToFloat(string);
	else
		properties.radius[2] = ed_lightEditor.editProperties.radius[2];

	if (GetWindowText(ed_lightEditor.hWndFovXValue, string, sizeof(string)))
		properties.fovX = Str_ToFloat(string);
	else
		properties.fovX = ed_lightEditor.editProperties.fovX;

	if (GetWindowText(ed_lightEditor.hWndFovYValue, string, sizeof(string)))
		properties.fovY = Str_ToFloat(string);
	else
		properties.fovY = ed_lightEditor.editProperties.fovY;

	if (GetWindowText(ed_lightEditor.hWndFalloffRangeValue, string, sizeof(string)))
		properties.falloffRange = Str_ToFloat(string);
	else
		properties.falloffRange = ed_lightEditor.editProperties.falloffRange;

	if (SendMessage(ed_lightEditor.hWndPointLight, BM_GETCHECK, 0, 0) == BST_CHECKED)
		properties.projected = false;
	else
		properties.projected = true;

	if (SendMessage(ed_lightEditor.hWndParallel, BM_GETCHECK, 0, 0) == BST_CHECKED)
		properties.parallel = true;
	else
		properties.parallel = false;

	if (SendMessage(ed_lightEditor.hWndCastShadows, BM_GETCHECK, 0, 0) == BST_UNCHECKED)
		properties.noShadows = true;
	else
		properties.noShadows = false;

	if (GetWindowText(ed_lightEditor.hWndDetailLevelValue, string, sizeof(string))){
		if (!Str_ICompare(string, "High"))
			properties.detailLevel = 2;
		else if (!Str_ICompare(string, "Medium"))
			properties.detailLevel = 1;
		else if (!Str_ICompare(string, "Low"))
			properties.detailLevel = 0;
		else
			properties.detailLevel = ed_lightEditor.editProperties.detailLevel;
	}
	else
		properties.detailLevel = ed_lightEditor.editProperties.detailLevel;

	properties.materialParms[0] = GetRValue(ed_lightEditor.crCurrent) * (1.0f/255);
	properties.materialParms[1] = GetGValue(ed_lightEditor.crCurrent) * (1.0f/255);
	properties.materialParms[2] = GetBValue(ed_lightEditor.crCurrent) * (1.0f/255);

	if (GetWindowText(ed_lightEditor.hWndAlphaValue, string, sizeof(string)))
		properties.materialParms[3] = Str_ToFloat(string);
	else
		properties.materialParms[3] = ed_lightEditor.editProperties.materialParms[3];

	properties.materialParms[4] = ed_lightEditor.editProperties.materialParms[4];
	properties.materialParms[5] = ed_lightEditor.editProperties.materialParms[5];

	if (GetWindowText(ed_lightEditor.hWndDepthValue, string, sizeof(string)))
		properties.materialParms[6] = Str_ToFloat(string);
	else
		properties.materialParms[6] = ed_lightEditor.editProperties.materialParms[6];

	properties.materialParms[7] = ed_lightEditor.editProperties.materialParms[7];

	if (!GetWindowText(ed_lightEditor.hWndMaterialName, properties.material, sizeof(properties.material))){
		if (properties.projected)
			Str_Copy(properties.material, "_defaultProjectedLight", sizeof(properties.material));
		else
			Str_Copy(properties.material, "_defaultPointLight", sizeof(properties.material));
	}

	// Update the properties
	ed_lightEditor.callbacks.Update(ed_lightEditor.editIndex, &properties);
}

/*
 ==================
 ED_ResetLightProperties
 ==================
*/
static void ED_ResetLightProperties (void){

	byte	r, g, b;
	int		i;

	if (!ed_lightEditor.enabled)
		return;

	// Set the current transform
	ed_lightEditor.currentTransform = 0;

	// Update the controls
	SetWindowText(ed_lightEditor.hWndOriginX, Str_VarArgs("%g", ed_lightEditor.editProperties.origin[0]));
	SetWindowText(ed_lightEditor.hWndOriginY, Str_VarArgs("%g", ed_lightEditor.editProperties.origin[1]));
	SetWindowText(ed_lightEditor.hWndOriginZ, Str_VarArgs("%g", ed_lightEditor.editProperties.origin[2]));

	SetWindowText(ed_lightEditor.hWndCenterX, Str_VarArgs("%g", ed_lightEditor.editProperties.center[0]));
	SetWindowText(ed_lightEditor.hWndCenterY, Str_VarArgs("%g", ed_lightEditor.editProperties.center[1]));
	SetWindowText(ed_lightEditor.hWndCenterZ, Str_VarArgs("%g", ed_lightEditor.editProperties.center[2]));

	SetWindowText(ed_lightEditor.hWndAnglesX, Str_VarArgs("%g", ed_lightEditor.editProperties.angles[0]));
	SetWindowText(ed_lightEditor.hWndAnglesY, Str_VarArgs("%g", ed_lightEditor.editProperties.angles[1]));
	SetWindowText(ed_lightEditor.hWndAnglesZ, Str_VarArgs("%g", ed_lightEditor.editProperties.angles[2]));

	SetWindowText(ed_lightEditor.hWndTransformValue, "Origin");

	if (ed_lightEditor.editProperties.projected)
		SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);

	SetWindowText(ed_lightEditor.hWndRadiusX, Str_VarArgs("%g", ed_lightEditor.editProperties.radius[0]));
	SetWindowText(ed_lightEditor.hWndRadiusY, Str_VarArgs("%g", ed_lightEditor.editProperties.radius[1]));
	SetWindowText(ed_lightEditor.hWndRadiusZ, Str_VarArgs("%g", ed_lightEditor.editProperties.radius[2]));

	if (ed_lightEditor.editProperties.projected)
		SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);

	SetWindowText(ed_lightEditor.hWndFovXValue, Str_VarArgs("%g", ed_lightEditor.editProperties.fovX));
	SetWindowText(ed_lightEditor.hWndFovYValue, Str_VarArgs("%g", ed_lightEditor.editProperties.fovY));

	SetWindowText(ed_lightEditor.hWndFalloffRangeValue, Str_VarArgs("%g", ed_lightEditor.editProperties.falloffRange));

	if (ed_lightEditor.editProperties.parallel)
		SendMessage(ed_lightEditor.hWndParallel, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndParallel, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);

	if (ed_lightEditor.editProperties.noShadows)
		SendMessage(ed_lightEditor.hWndCastShadows, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndCastShadows, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);

	SendMessage(ed_lightEditor.hWndDetailLevelValue, CB_SETCURSEL, (WPARAM)M_ClampLong(ed_lightEditor.editProperties.detailLevel, 0, 2), 0);

	r = M_ClampByte(M_Ftol(ed_lightEditor.editProperties.materialParms[0] * 255.0f));
	g = M_ClampByte(M_Ftol(ed_lightEditor.editProperties.materialParms[1] * 255.0f));
	b = M_ClampByte(M_Ftol(ed_lightEditor.editProperties.materialParms[2] * 255.0f));

	ed_lightEditor.crCurrent = RGB(r, g, b);

	for (i = 0; i < LIGHT_COLOR_BITMAP_SIZE; i++)
		ed_lightEditor.bitmapBits[i] = (b << 0) | (g << 8) | (r << 16) | (255 << 24);

	SetBitmapBits(ed_lightEditor.hBitmap, LIGHT_COLOR_BITMAP_SIZE * 4, ed_lightEditor.bitmapBits);
	SendMessage(ed_lightEditor.hWndColorSelect, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)ed_lightEditor.hBitmap);

	SetWindowText(ed_lightEditor.hWndAlphaValue, Str_VarArgs("%g", ed_lightEditor.editProperties.materialParms[3]));

	SetWindowText(ed_lightEditor.hWndDepthValue, Str_VarArgs("%g", ed_lightEditor.editProperties.materialParms[6]));

	SetWindowText(ed_lightEditor.hWndMaterialName, ed_lightEditor.editProperties.material);

	// Enable or disable the controls
	if (ed_lightEditor.editProperties.projected){
		EnableWindow(ed_lightEditor.hWndPointLight, TRUE);

		EnableWindow(ed_lightEditor.hWndRadius, FALSE);
		EnableWindow(ed_lightEditor.hWndRadiusX, FALSE);
		EnableWindow(ed_lightEditor.hWndRadiusY, FALSE);
		EnableWindow(ed_lightEditor.hWndRadiusZ, FALSE);

		EnableWindow(ed_lightEditor.hWndProjectedLight, FALSE);

		EnableWindow(ed_lightEditor.hWndFov, TRUE);
		EnableWindow(ed_lightEditor.hWndFovXValue, TRUE);
		EnableWindow(ed_lightEditor.hWndFovYValue, TRUE);

		EnableWindow(ed_lightEditor.hWndFalloffRange, TRUE);
		EnableWindow(ed_lightEditor.hWndFalloffRangeValue, TRUE);
	}
	else {
		EnableWindow(ed_lightEditor.hWndPointLight, FALSE);

		EnableWindow(ed_lightEditor.hWndRadius, TRUE);
		EnableWindow(ed_lightEditor.hWndRadiusX, TRUE);
		EnableWindow(ed_lightEditor.hWndRadiusY, TRUE);
		EnableWindow(ed_lightEditor.hWndRadiusZ, TRUE);

		EnableWindow(ed_lightEditor.hWndProjectedLight, TRUE);

		EnableWindow(ed_lightEditor.hWndFov, FALSE);
		EnableWindow(ed_lightEditor.hWndFovXValue, FALSE);
		EnableWindow(ed_lightEditor.hWndFovYValue, FALSE);

		EnableWindow(ed_lightEditor.hWndFalloffRange, FALSE);
		EnableWindow(ed_lightEditor.hWndFalloffRangeValue, FALSE);
	}

	// Update the properties
	ed_lightEditor.callbacks.Update(ed_lightEditor.editIndex, &ed_lightEditor.editProperties);
}

/*
 ==================
 ED_RemoveLightProperties
 ==================
*/
static void ED_RemoveLightProperties (void){

	byte	r, g, b;
	int		i;

	if (!ed_lightEditor.enabled)
		return;

	ed_lightEditor.enabled = false;

	// Set the current transform
	ed_lightEditor.currentTransform = 0;

	// Update the controls
	SetWindowText(ed_lightEditor.hWndOriginX, Str_VarArgs("%g", ed_lightProperties.origin[0]));
	SetWindowText(ed_lightEditor.hWndOriginY, Str_VarArgs("%g", ed_lightProperties.origin[1]));
	SetWindowText(ed_lightEditor.hWndOriginZ, Str_VarArgs("%g", ed_lightProperties.origin[2]));

	SetWindowText(ed_lightEditor.hWndCenterX, Str_VarArgs("%g", ed_lightProperties.center[0]));
	SetWindowText(ed_lightEditor.hWndCenterY, Str_VarArgs("%g", ed_lightProperties.center[1]));
	SetWindowText(ed_lightEditor.hWndCenterZ, Str_VarArgs("%g", ed_lightProperties.center[2]));

	SetWindowText(ed_lightEditor.hWndAnglesX, Str_VarArgs("%g", ed_lightProperties.angles[0]));
	SetWindowText(ed_lightEditor.hWndAnglesY, Str_VarArgs("%g", ed_lightProperties.angles[1]));
	SetWindowText(ed_lightEditor.hWndAnglesZ, Str_VarArgs("%g", ed_lightProperties.angles[2]));

	SetWindowText(ed_lightEditor.hWndTransformValue, "Origin");

	if (ed_lightProperties.projected)
		SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);

	SetWindowText(ed_lightEditor.hWndRadiusX, Str_VarArgs("%g", ed_lightProperties.radius[0]));
	SetWindowText(ed_lightEditor.hWndRadiusY, Str_VarArgs("%g", ed_lightProperties.radius[1]));
	SetWindowText(ed_lightEditor.hWndRadiusZ, Str_VarArgs("%g", ed_lightProperties.radius[2]));

	if (ed_lightProperties.projected)
		SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);

	SetWindowText(ed_lightEditor.hWndFovXValue, Str_VarArgs("%g", ed_lightProperties.fovX));
	SetWindowText(ed_lightEditor.hWndFovYValue, Str_VarArgs("%g", ed_lightProperties.fovY));

	SetWindowText(ed_lightEditor.hWndFalloffRangeValue, Str_VarArgs("%g", ed_lightProperties.falloffRange));

	if (ed_lightProperties.parallel)
		SendMessage(ed_lightEditor.hWndParallel, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndParallel, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);

	if (ed_lightProperties.noShadows)
		SendMessage(ed_lightEditor.hWndCastShadows, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndCastShadows, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);

	SendMessage(ed_lightEditor.hWndDetailLevelValue, CB_SETCURSEL, (WPARAM)M_ClampLong(ed_lightProperties.detailLevel, 0, 2), 0);

	r = M_ClampByte(M_Ftol(ed_lightProperties.materialParms[0] * 255.0f));
	g = M_ClampByte(M_Ftol(ed_lightProperties.materialParms[1] * 255.0f));
	b = M_ClampByte(M_Ftol(ed_lightProperties.materialParms[2] * 255.0f));

	ed_lightEditor.crCurrent = RGB(r, g, b);

	for (i = 0; i < LIGHT_COLOR_BITMAP_SIZE; i++)
		ed_lightEditor.bitmapBits[i] = (b << 0) | (g << 8) | (r << 16) | (255 << 24);

	SetBitmapBits(ed_lightEditor.hBitmap, LIGHT_COLOR_BITMAP_SIZE * 4, ed_lightEditor.bitmapBits);
	SendMessage(ed_lightEditor.hWndColorSelect, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)ed_lightEditor.hBitmap);

	SetWindowText(ed_lightEditor.hWndAlphaValue, Str_VarArgs("%g", ed_lightProperties.materialParms[3]));

	SetWindowText(ed_lightEditor.hWndDepthValue, Str_VarArgs("%g", ed_lightProperties.materialParms[6]));

	SetWindowText(ed_lightEditor.hWndMaterialName, ed_lightProperties.material);

	// Disable the controls
	EnableWindow(ed_lightEditor.hWndFrame1, FALSE);
	EnableWindow(ed_lightEditor.hWndFrame2, FALSE);
	EnableWindow(ed_lightEditor.hWndFrame3, FALSE);
	EnableWindow(ed_lightEditor.hWndFrame4, FALSE);
	EnableWindow(ed_lightEditor.hWndFrame5, FALSE);

	EnableWindow(ed_lightEditor.hWndX, FALSE);
	EnableWindow(ed_lightEditor.hWndY, FALSE);
	EnableWindow(ed_lightEditor.hWndZ, FALSE);

	EnableWindow(ed_lightEditor.hWndOrigin, FALSE);
	EnableWindow(ed_lightEditor.hWndOriginX, FALSE);
	EnableWindow(ed_lightEditor.hWndOriginY, FALSE);
	EnableWindow(ed_lightEditor.hWndOriginZ, FALSE);

	EnableWindow(ed_lightEditor.hWndCenter, FALSE);
	EnableWindow(ed_lightEditor.hWndCenterX, FALSE);
	EnableWindow(ed_lightEditor.hWndCenterY, FALSE);
	EnableWindow(ed_lightEditor.hWndCenterZ, FALSE);

	EnableWindow(ed_lightEditor.hWndAngles, FALSE);
	EnableWindow(ed_lightEditor.hWndAnglesX, FALSE);
	EnableWindow(ed_lightEditor.hWndAnglesY, FALSE);
	EnableWindow(ed_lightEditor.hWndAnglesZ, FALSE);

	EnableWindow(ed_lightEditor.hWndTransform, FALSE);
	EnableWindow(ed_lightEditor.hWndTransformValue, FALSE);

	EnableWindow(ed_lightEditor.hWndXInc, FALSE);
	EnableWindow(ed_lightEditor.hWndXDec, FALSE);
	EnableWindow(ed_lightEditor.hWndYInc, FALSE);
	EnableWindow(ed_lightEditor.hWndYDec, FALSE);
	EnableWindow(ed_lightEditor.hWndZInc, FALSE);
	EnableWindow(ed_lightEditor.hWndZDec, FALSE);

	EnableWindow(ed_lightEditor.hWndPointLight, FALSE);

	EnableWindow(ed_lightEditor.hWndRadius, FALSE);
	EnableWindow(ed_lightEditor.hWndRadiusX, FALSE);
	EnableWindow(ed_lightEditor.hWndRadiusY, FALSE);
	EnableWindow(ed_lightEditor.hWndRadiusZ, FALSE);

	EnableWindow(ed_lightEditor.hWndProjectedLight, FALSE);

	EnableWindow(ed_lightEditor.hWndFov, FALSE);
	EnableWindow(ed_lightEditor.hWndFovXValue, FALSE);
	EnableWindow(ed_lightEditor.hWndFovYValue, FALSE);

	EnableWindow(ed_lightEditor.hWndFalloffRange, FALSE);
	EnableWindow(ed_lightEditor.hWndFalloffRangeValue, FALSE);

	EnableWindow(ed_lightEditor.hWndParallel, FALSE);

	EnableWindow(ed_lightEditor.hWndCastShadows, FALSE);

	EnableWindow(ed_lightEditor.hWndDetailLevel, FALSE);
	EnableWindow(ed_lightEditor.hWndDetailLevelValue, FALSE);

	EnableWindow(ed_lightEditor.hWndColor, FALSE);
	EnableWindow(ed_lightEditor.hWndColorSelect, FALSE);

	EnableWindow(ed_lightEditor.hWndAlpha, FALSE);
	EnableWindow(ed_lightEditor.hWndAlphaValue, FALSE);

	EnableWindow(ed_lightEditor.hWndDepth, FALSE);
	EnableWindow(ed_lightEditor.hWndDepthValue, FALSE);

	EnableWindow(ed_lightEditor.hWndMaterial, FALSE);
	EnableWindow(ed_lightEditor.hWndMaterialName, FALSE);

	EnableWindow(ed_lightEditor.hWndApply, FALSE);
	EnableWindow(ed_lightEditor.hWndReset, FALSE);

	// Remove the properties
	ed_lightEditor.callbacks.Remove(ed_lightEditor.editIndex);
}

/*
 ==================
 ED_LightEditorWindowProc
 ==================
*/
static LRESULT CALLBACK ED_LightEditorWindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){

	CHOOSECOLOR	chooseColor;
	char		string[MAX_OSPATH];
	int			result;
	int			i;

	switch (uMsg){
	case WM_CREATE:
		sys.hWndEditor = hWnd;

		break;
	case WM_DESTROY:
		sys.hWndEditor = NULL;

		break;
	case WM_CLOSE:
		result = MessageBox(ed_lightEditor.hWnd, "Do you want to save the changes?", LIGHT_EDITOR_WINDOW_NAME, MB_YESNOCANCEL | MB_TASKMODAL);

		switch (result){
		case IDYES:
			ED_ApplyLightProperties();

			ed_lightEditor.callbacks.Save();
			ed_lightEditor.callbacks.Close();

			break;
		case IDNO:
			ED_ApplyLightProperties();

			ed_lightEditor.callbacks.Close();

			break;
		}

		return 0;
	case WM_COMMAND:
		if (HIWORD(wParam) == BN_CLICKED){
			if ((HWND)lParam == ed_lightEditor.hWndTransformValue){
				ed_lightEditor.currentTransform++;
				if (ed_lightEditor.currentTransform > 2)
					ed_lightEditor.currentTransform = 0;

				switch (ed_lightEditor.currentTransform){
				case 0:
					SetWindowText(ed_lightEditor.hWndTransformValue, "Origin");
					break;
				case 1:
					SetWindowText(ed_lightEditor.hWndTransformValue, "Center");
					break;
				case 2:
					SetWindowText(ed_lightEditor.hWndTransformValue, "Angles");
					break;
				}

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndXInc){
				switch (ed_lightEditor.currentTransform){
				case 0:
					GetWindowText(ed_lightEditor.hWndOriginX, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndOriginX, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				case 1:
					GetWindowText(ed_lightEditor.hWndCenterX, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndCenterX, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				case 2:
					GetWindowText(ed_lightEditor.hWndAnglesX, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndAnglesX, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				}

				ED_ApplyLightProperties();

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndXDec){
				switch (ed_lightEditor.currentTransform){
				case 0:
					GetWindowText(ed_lightEditor.hWndOriginX, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndOriginX, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				case 1:
					GetWindowText(ed_lightEditor.hWndCenterX, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndCenterX, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				case 2:
					GetWindowText(ed_lightEditor.hWndAnglesX, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndAnglesX, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				}

				ED_ApplyLightProperties();

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndYInc){
				switch (ed_lightEditor.currentTransform){
				case 0:
					GetWindowText(ed_lightEditor.hWndOriginY, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndOriginY, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				case 1:
					GetWindowText(ed_lightEditor.hWndCenterY, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndCenterY, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				case 2:
					GetWindowText(ed_lightEditor.hWndAnglesY, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndAnglesY, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				}

				ED_ApplyLightProperties();

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndYDec){
				switch (ed_lightEditor.currentTransform){
				case 0:
					GetWindowText(ed_lightEditor.hWndOriginY, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndOriginY, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				case 1:
					GetWindowText(ed_lightEditor.hWndCenterY, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndCenterY, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				case 2:
					GetWindowText(ed_lightEditor.hWndAnglesY, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndAnglesY, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				}

				ED_ApplyLightProperties();

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndZInc){
				switch (ed_lightEditor.currentTransform){
				case 0:
					GetWindowText(ed_lightEditor.hWndOriginZ, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndOriginZ, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				case 1:
					GetWindowText(ed_lightEditor.hWndCenterZ, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndCenterZ, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				case 2:
					GetWindowText(ed_lightEditor.hWndAnglesZ, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndAnglesZ, Str_VarArgs("%g", Str_ToFloat(string) + 1.0f));
					break;
				}

				ED_ApplyLightProperties();

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndZDec){
				switch (ed_lightEditor.currentTransform){
				case 0:
					GetWindowText(ed_lightEditor.hWndOriginZ, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndOriginZ, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				case 1:
					GetWindowText(ed_lightEditor.hWndCenterZ, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndCenterZ, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				case 2:
					GetWindowText(ed_lightEditor.hWndAnglesZ, string, sizeof(string));
					SetWindowText(ed_lightEditor.hWndAnglesZ, Str_VarArgs("%g", Str_ToFloat(string) - 1.0f));
					break;
				}

				ED_ApplyLightProperties();

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndPointLight){
				if (!ed_lightEditor.enabled)
					break;

				SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
				SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);

				EnableWindow(ed_lightEditor.hWndPointLight, FALSE);

				EnableWindow(ed_lightEditor.hWndRadius, TRUE);
				EnableWindow(ed_lightEditor.hWndRadiusX, TRUE);
				EnableWindow(ed_lightEditor.hWndRadiusY, TRUE);
				EnableWindow(ed_lightEditor.hWndRadiusZ, TRUE);

				EnableWindow(ed_lightEditor.hWndProjectedLight, TRUE);

				EnableWindow(ed_lightEditor.hWndFov, FALSE);
				EnableWindow(ed_lightEditor.hWndFovXValue, FALSE);
				EnableWindow(ed_lightEditor.hWndFovYValue, FALSE);

				EnableWindow(ed_lightEditor.hWndFalloffRange, FALSE);
				EnableWindow(ed_lightEditor.hWndFalloffRangeValue, FALSE);

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndProjectedLight){
				if (!ed_lightEditor.enabled)
					break;

				SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
				SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);

				EnableWindow(ed_lightEditor.hWndPointLight, TRUE);

				EnableWindow(ed_lightEditor.hWndRadius, FALSE);
				EnableWindow(ed_lightEditor.hWndRadiusX, FALSE);
				EnableWindow(ed_lightEditor.hWndRadiusY, FALSE);
				EnableWindow(ed_lightEditor.hWndRadiusZ, FALSE);

				EnableWindow(ed_lightEditor.hWndProjectedLight, FALSE);

				EnableWindow(ed_lightEditor.hWndFov, TRUE);
				EnableWindow(ed_lightEditor.hWndFovXValue, TRUE);
				EnableWindow(ed_lightEditor.hWndFovYValue, TRUE);

				EnableWindow(ed_lightEditor.hWndFalloffRange, TRUE);
				EnableWindow(ed_lightEditor.hWndFalloffRangeValue, TRUE);

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndColorSelect){
				chooseColor.lStructSize = sizeof(CHOOSECOLOR);
				chooseColor.hwndOwner = ed_lightEditor.hWnd;
				chooseColor.hInstance = NULL;
				chooseColor.rgbResult = ed_lightEditor.crCurrent;
				chooseColor.lpCustColors = ed_lightEditor.crCustom;
				chooseColor.Flags = CC_FULLOPEN | CC_RGBINIT;
				chooseColor.lCustData = 0;
				chooseColor.lpfnHook = NULL;
				chooseColor.lpTemplateName = NULL;

/*
				if (ChooseColor(&chooseColor)){
					ed_lightEditor.crCurrent = chooseColor.rgbResult;

					for (i = 0; i < LIGHT_COLOR_BITMAP_SIZE; i++)
						ed_lightEditor.bitmapBits[i] = (GetBValue(chooseColor.rgbResult) << 0) | (GetGValue(chooseColor.rgbResult) << 8) | (GetRValue(chooseColor.rgbResult) << 16) | (255 << 24);

					SetBitmapBits(ed_lightEditor.hBitmap, LIGHT_COLOR_BITMAP_SIZE * 4, ed_lightEditor.bitmapBits);
					SendMessage(ed_lightEditor.hWndColorSelect, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)ed_lightEditor.hBitmap);
				}
*/
				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndApply){
				ED_ApplyLightProperties();
				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndReset){
				ED_ResetLightProperties();
				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndRemove){
				result = MessageBox(ed_lightEditor.hWnd, "Do you want to remove all lights?", LIGHT_EDITOR_WINDOW_NAME, MB_YESNOCANCEL | MB_TASKMODAL);

				ED_RemoveLightProperties();

				if (result == IDYES)
					ed_lightEditor.callbacks.Clear();

				break;
			}

			if ((HWND)lParam == ed_lightEditor.hWndSave){
				ED_ApplyLightProperties();

				ed_lightEditor.callbacks.Save();

				break;
			}
		}

		break;
	}

	// Pass all unhandled messages to DefWindowProc
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

/*
 ==================
 ED_CreateLightEditorWindow
 ==================
*/
bool ED_CreateLightEditorWindow (void){

	WNDCLASSEX	wndClass;
	RECT		rect;
	int			screenWidth, screenHeight;
	int			x, y, w, h;
	int			i;

	// Center the window in the desktop
	screenWidth = GetSystemMetrics(SM_CXSCREEN);
	screenHeight = GetSystemMetrics(SM_CYSCREEN);

	rect.left = (screenWidth - 416) / 2;
	rect.top = (screenHeight - 378) / 2;
	rect.right = rect.left + 416;
	rect.bottom = rect.top + 378;

	AdjustWindowRectEx(&rect, LIGHT_EDITOR_WINDOW_STYLE, FALSE, 0);

	x = rect.left;
	y = rect.top;
	w = rect.right - rect.left;
	h = rect.bottom - rect.top;

	// Register the frame class
	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.style = 0;
	wndClass.lpfnWndProc = ED_LightEditorWindowProc;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hInstance = sys.hInstance;
	wndClass.hIcon = LoadIcon(sys.hInstance, MAKEINTRESOURCE(IDI_ICON1));
	wndClass.hIconSm = 0;
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
	wndClass.lpszMenuName = 0;
	wndClass.lpszClassName = LIGHT_EDITOR_WINDOW_CLASS;

	if (!RegisterClassEx(&wndClass))
		return false;

	// Create the window
	ed_lightEditor.hWnd = CreateWindowEx(0, LIGHT_EDITOR_WINDOW_CLASS, LIGHT_EDITOR_WINDOW_NAME, LIGHT_EDITOR_WINDOW_STYLE, x, y, w, h, NULL, NULL, sys.hInstance, NULL);
	if (!ed_lightEditor.hWnd){
		UnregisterClass(LIGHT_EDITOR_WINDOW_CLASS, sys.hInstance);
		return false;
	}

	// Create the controls
	ed_lightEditor.hWndFrame1 = CreateWindowEx(0, "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 8, 2, 196, 108, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndFrame2 = CreateWindowEx(0, "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 212, 2, 196, 108, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndFrame3 = CreateWindowEx(0, "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 8, 112, 196, 184, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndFrame4 = CreateWindowEx(0, "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 212, 112, 196, 184, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndFrame5 = CreateWindowEx(0, "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 8, 298, 400, 35, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndX = CreateWindowEx(0, "STATIC", "X", WS_CHILD | WS_VISIBLE | SS_CENTER, 54, 12, 46, 14, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndY = CreateWindowEx(0, "STATIC", "Y", WS_CHILD | WS_VISIBLE | SS_CENTER, 104, 12, 46, 14, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndZ = CreateWindowEx(0, "STATIC", "Z", WS_CHILD | WS_VISIBLE | SS_CENTER, 154, 12, 46, 14, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndOrigin = CreateWindowEx(0, "STATIC", "Origin", WS_CHILD | WS_VISIBLE | SS_RIGHT, 12, 29, 38, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndOriginX = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 54, 26, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndOriginY = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 104, 26, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndOriginZ = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 154, 26, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndCenter = CreateWindowEx(0, "STATIC", "Center", WS_CHILD | WS_VISIBLE | SS_RIGHT, 12, 59, 38, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndCenterX = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 54, 56, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndCenterY = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 104, 56, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndCenterZ = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 154, 56, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndAngles = CreateWindowEx(0, "STATIC", "Angles", WS_CHILD | WS_VISIBLE | SS_RIGHT, 12, 89, 38, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndAnglesX = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 54, 86, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndAnglesY = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 104, 86, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndAnglesZ = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 154, 86, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndTransform = CreateWindowEx(0, "STATIC", "Position / Orientation:", WS_CHILD | WS_VISIBLE | SS_LEFT, 216, 12, 188, 14, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndTransformValue = CreateWindowEx(0, "BUTTON", "Origin", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 216, 56, 46, 24, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndXInc = CreateWindowEx(0, "BUTTON", "X+", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 340, 56, 24, 24, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndXDec = CreateWindowEx(0, "BUTTON", "X-", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 288, 56, 24, 24, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndYInc = CreateWindowEx(0, "BUTTON", "Y+", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 314, 30, 24, 24, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndYDec = CreateWindowEx(0, "BUTTON", "Y-", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 314, 82, 24, 24, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndZInc = CreateWindowEx(0, "BUTTON", "Z+", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 380, 30, 24, 24, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndZDec = CreateWindowEx(0, "BUTTON", "Z-", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 380, 82, 24, 24, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndPointLight = CreateWindowEx(0, "BUTTON", "Point light", WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON, 12, 122, 188, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndRadius = CreateWindowEx(0, "STATIC", "Radius", WS_CHILD | WS_VISIBLE | SS_RIGHT, 12, 145, 38, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndRadiusX = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 54, 142, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndRadiusY = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 104, 142, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndRadiusZ = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 154, 142, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndProjectedLight = CreateWindowEx(0, "BUTTON", "Projected light", WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON, 12, 222, 188, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndFov = CreateWindowEx(0, "STATIC", "FOV", WS_CHILD | WS_VISIBLE | SS_RIGHT, 12, 245, 88, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndFovXValue = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 104, 242, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndFovYValue = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 154, 242, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndFalloffRange = CreateWindowEx(0, "STATIC", "Falloff range", WS_CHILD | WS_VISIBLE | SS_RIGHT, 12, 275, 138, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndFalloffRangeValue = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 154, 272, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndParallel = CreateWindowEx(0, "BUTTON", "Parallel", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, 216, 122, 188, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndCastShadows = CreateWindowEx(0, "BUTTON", "Cast shadows", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_AUTOCHECKBOX, 216, 142, 188, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndDetailLevel = CreateWindowEx(0, "STATIC", "Detail level", WS_CHILD | WS_VISIBLE | SS_RIGHT, 216, 175, 118, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndDetailLevelValue = CreateWindowEx(0, "COMBOBOX", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWNLIST, 338, 172, 66, 200, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndColor = CreateWindowEx(0, "STATIC", "Color", WS_CHILD | WS_VISIBLE | SS_RIGHT, 216, 205, 118, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndColorSelect = CreateWindowEx(0, "BUTTON", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER | BS_BITMAP, 338, 202, 66, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndAlpha = CreateWindowEx(0, "STATIC", "Blend alpha / Fog distance", WS_CHILD | WS_VISIBLE | SS_RIGHT, 216, 245, 138, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndAlphaValue = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 358, 242, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndDepth = CreateWindowEx(0, "STATIC", "Fog depth", WS_CHILD | WS_VISIBLE | SS_RIGHT, 216, 275, 138, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndDepthValue = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL, 358, 272, 46, 20, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndMaterial = CreateWindowEx(0, "STATIC", "Material", WS_CHILD | WS_VISIBLE | SS_RIGHT, 12, 311, 88, 16, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndMaterialName = CreateWindowEx(0, "COMBOBOX", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | CBS_DROPDOWN | CBS_SORT | CBS_AUTOHSCROLL, 104, 308, 302, 200, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	ed_lightEditor.hWndApply = CreateWindowEx(0, "BUTTON", "Apply", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 8, 345, 80, 25, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndReset = CreateWindowEx(0, "BUTTON", "Reset", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 92, 345, 80, 25, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndRemove = CreateWindowEx(0, "BUTTON", "Remove", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 244, 345, 80, 25, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);
	ed_lightEditor.hWndSave = CreateWindowEx(0, "BUTTON", "Save", WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, 328, 345, 80, 25, ed_lightEditor.hWnd, NULL, sys.hInstance, NULL);

	// Create and set the font
	ed_lightEditor.hFont = CreateFont(14, 0, 0, 0, FW_LIGHT, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "MS Sans Serif");

	SendMessage(ed_lightEditor.hWndFrame1, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndFrame2, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndFrame3, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndFrame4, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndFrame5, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndX, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndY, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndZ, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndOrigin, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndOriginX, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndOriginY, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndOriginZ, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndCenter, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndCenterX, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndCenterY, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndCenterZ, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndAngles, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndAnglesX, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndAnglesY, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndAnglesZ, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndTransform, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndTransformValue, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndXInc, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndXDec, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndYInc, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndYDec, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndZInc, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndZDec, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndPointLight, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndRadius, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndRadiusX, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndRadiusY, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndRadiusZ, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndProjectedLight, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndFov, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndFovXValue, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndFovYValue, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndFalloffRange, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndFalloffRangeValue, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndParallel, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndCastShadows, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndDetailLevel, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndDetailLevelValue, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndColor, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndColorSelect, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndAlpha, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndAlphaValue, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndDepth, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndDepthValue, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndMaterial, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndMaterialName, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	SendMessage(ed_lightEditor.hWndApply, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndReset, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndRemove, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);
	SendMessage(ed_lightEditor.hWndSave, WM_SETFONT, (WPARAM)ed_lightEditor.hFont, FALSE);

	// Create and set the bitmap
	ed_lightEditor.hBitmap = CreateBitmap(66, 20, 1, 32, NULL);

	for (i = 0; i < LIGHT_COLOR_BITMAP_SIZE; i++)
		ed_lightEditor.bitmapBits[i] = 0xFFFFFFFF;

	SetBitmapBits(ed_lightEditor.hBitmap, LIGHT_COLOR_BITMAP_SIZE * 4, ed_lightEditor.bitmapBits);
	SendMessage(ed_lightEditor.hWndColorSelect, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)ed_lightEditor.hBitmap);

	// Fill detail level combo box
	SendMessage(ed_lightEditor.hWndDetailLevelValue, CB_ADDSTRING, 0, (LPARAM)"Low");
	SendMessage(ed_lightEditor.hWndDetailLevelValue, CB_ADDSTRING, 0, (LPARAM)"Medium");
	SendMessage(ed_lightEditor.hWndDetailLevelValue, CB_ADDSTRING, 0, (LPARAM)"High");

	// Fill material combo box
	SendMessage(ed_lightEditor.hWndMaterialName, CB_ADDSTRING, 0, (LPARAM)"_defaultPointLight");
	SendMessage(ed_lightEditor.hWndMaterialName, CB_ADDSTRING, 0, (LPARAM)"_defaultProjectedLight");

	R_EnumMaterialScripts(ED_AddLightMaterial);

	// Set text limit for material combo box
	SendMessage(ed_lightEditor.hWndMaterialName, CB_LIMITTEXT, (WPARAM)(MAX_OSPATH-1), 0);

	// Set the current color
	ed_lightEditor.crCurrent = RGB(255, 255, 255);

	// Set the custom colors
	for (i = 0; i < 16; i++)
		ed_lightEditor.crCustom[i] = RGB(255, 255, 255);

	// Show the window
	ShowWindow(ed_lightEditor.hWnd, SW_SHOW);
	UpdateWindow(ed_lightEditor.hWnd);
	SetForegroundWindow(ed_lightEditor.hWnd);
	SetFocus(ed_lightEditor.hWnd);

	return true;
}

/*
 ==================
 ED_DestroyLightEditorWindow
 ==================
*/
void ED_DestroyLightEditorWindow (void){

	if (!ed_lightEditor.hWnd)
		return;

	if (ed_lightEditor.hBitmap)
		DeleteObject(ed_lightEditor.hBitmap);

	if (ed_lightEditor.hFont)
		DeleteObject(ed_lightEditor.hFont);

	ShowWindow(ed_lightEditor.hWnd, SW_HIDE);
	DestroyWindow(ed_lightEditor.hWnd);
	UnregisterClass(LIGHT_EDITOR_WINDOW_CLASS, sys.hInstance);

	memset(&ed_lightEditor, 0, sizeof(lightEditor_t));
}

/*
 ==================
 ED_SetLightCallbacks
 ==================
*/
void ED_SetLightCallbacks (lightCallbacks_t *callbacks){

	if (!ed_lightEditor.hWnd)
		return;

	ed_lightEditor.callbacks.Update = callbacks->Update;
	ed_lightEditor.callbacks.Remove = callbacks->Remove;
	ed_lightEditor.callbacks.Clear = callbacks->Clear;
	ed_lightEditor.callbacks.Save = callbacks->Save;
	ed_lightEditor.callbacks.Close = callbacks->Close;
}

/*
 ==================
 ED_EditLightProperties
 ==================
*/
void ED_EditLightProperties (int index, lightProperties_t *properties){

	byte	r, g, b;
	int		i;

	if (!ed_lightEditor.hWnd)
		return;

	// Apply current light properties
	ED_ApplyLightProperties();

	// Set the current light properties
	if (properties){
		ed_lightEditor.enabled = true;

		ed_lightEditor.editIndex = index;
		ed_lightEditor.editProperties = *properties;
	}
	else {
		ed_lightEditor.enabled = false;

		properties = &ed_lightProperties;
	}

	// Set the current transform
	ed_lightEditor.currentTransform = 0;

	// Update the controls
	SetWindowText(ed_lightEditor.hWndOriginX, Str_VarArgs("%g", properties->origin[0]));
	SetWindowText(ed_lightEditor.hWndOriginY, Str_VarArgs("%g", properties->origin[1]));
	SetWindowText(ed_lightEditor.hWndOriginZ, Str_VarArgs("%g", properties->origin[2]));

	SetWindowText(ed_lightEditor.hWndCenterX, Str_VarArgs("%g", properties->center[0]));
	SetWindowText(ed_lightEditor.hWndCenterY, Str_VarArgs("%g", properties->center[1]));
	SetWindowText(ed_lightEditor.hWndCenterZ, Str_VarArgs("%g", properties->center[2]));

	SetWindowText(ed_lightEditor.hWndAnglesX, Str_VarArgs("%g", properties->angles[0]));
	SetWindowText(ed_lightEditor.hWndAnglesY, Str_VarArgs("%g", properties->angles[1]));
	SetWindowText(ed_lightEditor.hWndAnglesZ, Str_VarArgs("%g", properties->angles[2]));

	SetWindowText(ed_lightEditor.hWndTransformValue, "Origin");

	if (properties->projected)
		SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndPointLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);

	SetWindowText(ed_lightEditor.hWndRadiusX, Str_VarArgs("%g", properties->radius[0]));
	SetWindowText(ed_lightEditor.hWndRadiusY, Str_VarArgs("%g", properties->radius[1]));
	SetWindowText(ed_lightEditor.hWndRadiusZ, Str_VarArgs("%g", properties->radius[2]));

	if (properties->projected)
		SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndProjectedLight, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);

	SetWindowText(ed_lightEditor.hWndFovXValue, Str_VarArgs("%g", properties->fovX));
	SetWindowText(ed_lightEditor.hWndFovYValue, Str_VarArgs("%g", properties->fovY));

	SetWindowText(ed_lightEditor.hWndFalloffRangeValue, Str_VarArgs("%g", properties->falloffRange));

	if (properties->parallel)
		SendMessage(ed_lightEditor.hWndParallel, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndParallel, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);

	if (properties->noShadows)
		SendMessage(ed_lightEditor.hWndCastShadows, BM_SETCHECK, (WPARAM)BST_UNCHECKED, 0);
	else
		SendMessage(ed_lightEditor.hWndCastShadows, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);

	SendMessage(ed_lightEditor.hWndDetailLevelValue, CB_SETCURSEL, (WPARAM)M_ClampLong(properties->detailLevel, 0, 2), 0);

	r = M_ClampByte(M_Ftol(properties->materialParms[0] * 255.0f));
	g = M_ClampByte(M_Ftol(properties->materialParms[1] * 255.0f));
	b = M_ClampByte(M_Ftol(properties->materialParms[2] * 255.0f));

	ed_lightEditor.crCurrent = RGB(r, g, b);

	for (i = 0; i < LIGHT_COLOR_BITMAP_SIZE; i++)
		ed_lightEditor.bitmapBits[i] = (b << 0) | (g << 8) | (r << 16) | (255 << 24);

	SetBitmapBits(ed_lightEditor.hBitmap, LIGHT_COLOR_BITMAP_SIZE * 4, ed_lightEditor.bitmapBits);
	SendMessage(ed_lightEditor.hWndColorSelect, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)ed_lightEditor.hBitmap);

	SetWindowText(ed_lightEditor.hWndAlphaValue, Str_VarArgs("%g", properties->materialParms[3]));

	SetWindowText(ed_lightEditor.hWndDepthValue, Str_VarArgs("%g", properties->materialParms[6]));

	SetWindowText(ed_lightEditor.hWndMaterialName, properties->material);

	// Enable or disable the controls
	EnableWindow(ed_lightEditor.hWndFrame1, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndFrame2, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndFrame3, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndFrame4, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndFrame5, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndX, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndY, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndZ, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndOrigin, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndOriginX, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndOriginY, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndOriginZ, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndCenter, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndCenterX, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndCenterY, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndCenterZ, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndAngles, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndAnglesX, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndAnglesY, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndAnglesZ, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndTransform, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndTransformValue, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndXInc, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndXDec, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndYInc, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndYDec, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndZInc, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndZDec, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndPointLight, ed_lightEditor.enabled && properties->projected);

	EnableWindow(ed_lightEditor.hWndRadius, ed_lightEditor.enabled && !properties->projected);
	EnableWindow(ed_lightEditor.hWndRadiusX, ed_lightEditor.enabled && !properties->projected);
	EnableWindow(ed_lightEditor.hWndRadiusY, ed_lightEditor.enabled && !properties->projected);
	EnableWindow(ed_lightEditor.hWndRadiusZ, ed_lightEditor.enabled && !properties->projected);

	EnableWindow(ed_lightEditor.hWndProjectedLight, ed_lightEditor.enabled && !properties->projected);

	EnableWindow(ed_lightEditor.hWndFov, ed_lightEditor.enabled && properties->projected);
	EnableWindow(ed_lightEditor.hWndFovXValue, ed_lightEditor.enabled && properties->projected);
	EnableWindow(ed_lightEditor.hWndFovYValue, ed_lightEditor.enabled && properties->projected);

	EnableWindow(ed_lightEditor.hWndFalloffRange, ed_lightEditor.enabled && properties->projected);
	EnableWindow(ed_lightEditor.hWndFalloffRangeValue, ed_lightEditor.enabled && properties->projected);

	EnableWindow(ed_lightEditor.hWndParallel, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndCastShadows, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndDetailLevel, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndDetailLevelValue, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndColor, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndColorSelect, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndAlpha, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndAlphaValue, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndDepth, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndDepthValue, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndMaterial, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndMaterialName, ed_lightEditor.enabled);

	EnableWindow(ed_lightEditor.hWndApply, ed_lightEditor.enabled);
	EnableWindow(ed_lightEditor.hWndReset, ed_lightEditor.enabled);

	// Show the window
	ShowWindow(ed_lightEditor.hWnd, SW_SHOW);
	UpdateWindow(ed_lightEditor.hWnd);
	SetForegroundWindow(ed_lightEditor.hWnd);
	SetFocus(ed_lightEditor.hWnd);
}
